from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from goods.ser import *

from goods.models import *


# Create your views here.
# 获取商品分类数据
class ProductCategoryView(APIView):
    def get(self, request):
        category = ProductCategoryModel.objects.all()
        ser = ProductCategorySerializer(category, many=True)
        return Response({'code': 200, 'mes': '成功获取商品类别', 'data': ser.data})


# 展示品牌表数据
class ProductBrandView(APIView):
    def get(self, request):
        brand = ProductBrandModel.objects.all()
        ser = ProductBrandSerializer(brand, many=True)
        return Response({'code': 200, 'mes': '成功获取品牌数据', 'data': ser.data})


# 点击分类表中的id展示这个id下的所有数据
class ProductListView(APIView):
    def get(self, request, category_id):
        products = ProductModel.objects.filter(category_id=category_id)
        ser = ProductSerializer(products, many=True)
        return Response({'code': 200, 'mes': '成功获取该类目下的所有数据', 'data': ser.data})


# 点击品牌表中的id展示这个id下的所有数据
class ProductBrandListView(APIView):
    def get(self, request, brand_id):
        products = ProductModel.objects.filter(brand_id=brand_id)
        ser = ProductSerializer(products, many=True)
        return Response({'code': 200, 'mes': '成功获取该品牌下的所有数据', 'data': ser.data})
